package com.aier.admin_new.common;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.admin_new.service.IChannelClient;
import com.aier.admin_new.service.impl.ChannelClientImpl;
import com.aier.user.entity.User;
import com.baidu.yun.channel.model.BindInfo;

/**
 * @author Wkk300
 */
public class MessageBaiduYunSendTransmitter  {	
	
	private final ExecutorService executorService;//线程池
    
	private User user;
    private String deviceType;
    private Long channelId;
    private String UserId;
    private String tagName;
    private String title;
    private String description;
    private int type;
    private Long uid;//单推用
    private int types;// 1 2 3
   
   
    public MessageBaiduYunSendTransmitter() {
		super();
		executorService = Executors.newFixedThreadPool(15);
	}
	
    /**
     * 组推
     * @param tagName
     * @param title
     * @param description
     * @param type
     * @param uid
     * @param types
     */
	public MessageBaiduYunSendTransmitter(String tagName,String title,String description,int type, Long uid,int types) {
		this();
		this.tagName = tagName;
		this.title = title;
		this.description = description;
		this.uid = uid;
		this.type=type;
		this.types=types;
	}
	
	/**
	 * 单推
	 * @param user
	 * @param title
	 * @param description
	 * @param type
	 * @param uid
	 * @param types
	 */
	public MessageBaiduYunSendTransmitter(User user,String title,String description,int type, Long uid,int types) {
		this();
		this.user = user;
		this.title = title;
		this.description = description;
		this.type = type;
		this.uid = uid;
		this.types=types;
	}
	
	/**
	 * 单推安卓和IOS区分
	 * @param deviceType
	 * @param channelId
	 * @param UserId
	 * @param title
	 * @param description
	 * @param type
	 * @param uid
	 * @param types
	 */
	public MessageBaiduYunSendTransmitter(String deviceType,Long channelId,String UserId,String title,String description,int type, Long uid,int types) {
		this();
		this.deviceType=deviceType;
		this.channelId = channelId;
		this.title = title;
		this.description = description;
		this.type = type;
		this.uid = uid;
		this.types=types;
	}
	

	public void sendServer() {
		executorService.execute(new Task());
		executorService.shutdown();
	}

	
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getDeviceType() {
		return deviceType;
	}

	public void setDeviceType(String deviceType) {
		this.deviceType = deviceType;
	}

	public Long getChannelId() {
		return channelId;
	}

	public void setChannelId(Long channelId) {
		this.channelId = channelId;
	}

	public String getUserId() {
		return UserId;
	}

	public void setUserId(String userId) {
		UserId = userId;
	}

	public String getTagName() {
		return tagName;
	}

	public void setTagName(String tagName) {
		this.tagName = tagName;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public Long getUid() {
		return uid;
	}

	public void setUid(Long uid) {
		this.uid = uid;
	}

	public int getTypes() {
		return types;
	}

	public void setTypes(int types) {
		this.types = types;
	}



	class Task implements Runnable{
		@Override
		public void run() {
			try{
				IChannelClient	channelClient =new ChannelClientImpl();
				if(types==1){
						channelClient.PushTagMessage(tagName, title, description, type, uid);//1组推
				}
				if(types==2){
					List<BindInfo> bf= channelClient.QueryBindList(user.getAppUid());
					if(bf.size()>0 | bf!=null){
						channelClient.PushUnicastMessage(bf.get(0).getChannelId(), bf.get(0).getUserId(), title, description, type, uid);//2单推信息
					}
				}
				if(types==3){
					channelClient.PushUnicastNotification(deviceType, channelId, UserId, title, description, type, uid);//3单推状态栏信息
				}
			}catch(Exception e){
				e.getStackTrace();
				System.out.println(e.getStackTrace());
			}
			
		}
	}
}